package com.shijie.controller.app;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.component.AppUserService;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.domain.entity.Activitys;
import com.shijie.domain.entity.Coupons;
import com.shijie.domain.entity.UserAddress;
import com.shijie.domain.entity.UserCoupons;
import com.shijie.domain.enums.EnumInterface;
import com.shijie.service.ActivitysService;
import com.shijie.service.CouponsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/*
 *  优惠券活动相关接口
 *
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/app/coupons")
public class AppCouponsController {

        @Resource
        private CouponsService couponsService;

        @Resource
        private AppUserService appUserService;

        /**
         *
         * @param page
         * @param pageSize
         * @param status    0 待使用， 1已使用 ， 2 已过期
         * @param brandId   0 全平台
         * @return
         */
        @GetMapping
        public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer pageSize
                , @RequestParam(defaultValue = "0") Integer status, @RequestParam(defaultValue = "0") Integer brandId) {
                PageHelper.startPage(page, pageSize);
                List<UserCoupons> list = couponsService.selectByBrandId(appUserService.getCurrentUser().getId(), brandId, status);
                PageInfo<UserCoupons> pageInfo = new PageInfo<>(list);
                return ResultGenerator.success(pageInfo);
        }


        /**
         * 获取到可用的优惠券
         *
         * @param page
         * @param pageSize
         * @param brandId 用逗号隔开 brandId,brandId,brandId
         * @return
         */
        @GetMapping("@brandId")
        public Result listByBrandId(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "5") Integer pageSize
                , @RequestParam(required = false) String brandId) {
                PageHelper.startPage(page, pageSize);
                Condition condition = new Condition(Coupons.class);
                Example.Criteria criteria = condition.createCriteria();
                criteria.andLessThanOrEqualTo("startDate", new Date());
                criteria.andGreaterThanOrEqualTo("endDate", new Date());
                if (StringUtils.isNotBlank(brandId)) {
                        criteria.andIn("brandId", new ArrayList<>(Arrays.asList(brandId.split(","))));
                }
                criteria.andEqualTo("status", EnumInterface.Status.VALID);
                condition.orderBy("id").desc();
                List<Coupons> list = couponsService.selectByCondition(condition);
                PageInfo<Coupons> pageInfo = new PageInfo<>(list);
                return ResultGenerator.success(pageInfo);
        }


        @PutMapping
        public Result insert(@RequestBody UserCoupons userCoupons) {
                userCoupons.setUserId(appUserService.getCurrentUser().getId());
                String msg = couponsService.InsertUserCoupons(userCoupons);
                if (StringUtils.isBlank(msg)) {
                        return ResultGenerator.success();
                } else {
                        return ResultGenerator.error(msg);
                }

        }
}

